<?php
class SocialController extends SilkControllerBase {
	public function __construct( ) {
		parent::__construct();
	}
	public function ListShouts( $params ) {
		$lastweek = new DateTime();
		$lastweek->modify( '-1 week' );
		$shouts = orm( 'Shout' )->find_all_by_portfolio_id( $params['portfolio_id'], array('order' => 'created DESC', 'conditions' => array( "created >= ?", $lastweek->format('Y-m-d H:i:s') ) ) );
		$this->set( 'shouts', $shouts );
	}
	public function ShoutItem( $params ) {
		$this->set( 'ShoutItem', $params['ShoutItem'] );
	}
	public function Shout( $params ) {
		//$this->set( 'user_id', empty( $current_user->id ) ? 0 : $current_user->id );
		$current_user = SilkUserSession::get_current_user();
		if( isset( $params['message'] ) ) {
			//save message
			if( @$current_user->id > 0 ) {
				$shout = orm( 'Shout' );
				$shout->message = $params['message'];
				$shout->user_id = $current_user->id;
				$shout->portfolio_id = $params['portfolio_id'];
				$shout->created = date( "Y-m-d H:i:s" );
				$shout->save();
				
				if( isset( $params['is_silk_ajax'] ) ) {
					$response = new SilkAjax();
					$response->insert( '#ListShouts', "<div class=\"shout\"><h4>{$shout->user->username}</h4>{$shout->message}<div class=\"shout_created\"></div></div>"/*$this->ShoutItem( array( 'ShoutItem'=>$shout ) )*/, 'prepend' );
					$response->script( "document.getElementById('form_shoutform').reset();" );
					return $response->get_result();
				}
			}else{
				
			}
		}else{
			$this->set( 'AllowedToShout', @$current_user->id > 0 );
			$this->set( 'ErrorMessage', 'Please login to leave a message' );
			$this->set( 'portfolio_id', $params['portfolio_id'] );
		}
	}
	/**
	 * Return an array of user objects of $params["user_id"]'s friends
	 *
	 * @param unknown_type $params
	 * @return unknown
	 */
	public function getFriendsActivity($params) {

		$user = MpController::get_user($params["user_id"]);
		
		$friendsActivity = orm("UserActivity")->find_all(array(	"conditions" => array("silk_friends.user_id = $params[user_id]"),
				"joins" => "INNER JOIN silk_friends on silk_friends.friend_id = silk_user_activity.user_id",
				"order" => "create_date DESC",
				"limit" => $params["limit"]));
		return $friendsActivity;
	}
}
?>